var http = require('http');
var fs = require('fs');
var url = require('url');
var querystring = require('querystring');

var server = http.createServer(function(req,res){
	var pathname = url.parse(req.url).pathname;
	if(pathname == "/index"){
		var pageContent = fs.readFile("index.html","utf8",function(err,data){
			if(err){
				console.log("ERROR: "+ err);
			}else{
				res.writeHead(200,{"Content-Type":"text/html"});
				res.write(data);
				res.end();
			}
		});
	}else if(pathname == "/post"){
		var post = '';
		req.on('data',function(chunk){
			post += chunk;
			console.log(post);
		});
		req.on('end',function(){
			post = querystring.parse(post);
			res.writeHead(200,{"Content-Type":"text/html"});
			res.write("Your name is : " + post.name + "<br/>");
			res.write("Your email is : " + post.email + "<br/>");
			res.write("Your address is : " + post.address + "<hr/>");
			res.end("That's all data!");
		});
	}else{
		res.writeHead(200,{"Content-Type":"text/html"});
		res.end("<h1>I can't find any page</h1>");
	}
});

server.listen(8888);
console.log("Please visit the home page @http://localhost:8888/index");